package com.example.hotelsystem.service.impl;

import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hotelsystem.entity.User;
import com.example.hotelsystem.entity.UserRole;
import com.example.hotelsystem.mapper.UserRoleMapper;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.UserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lee
 * @since 2020-10-13
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    @Autowired
    UserRoleService UserRoleService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result ChangeUserRole(Long userId, Long roleId){
        QueryWrapper<UserRole> roleQuery = new QueryWrapper<>();
        roleQuery.eq("user_id",userId);
        UserRole userRole = UserRoleService.getOne(roleQuery);
        userRole.setRoleId(roleId);
        boolean isChange = UserRoleService.updateById(userRole);
        if(isChange){
            return Result.success("修改成功");
        }else{
            return Result.error(CodeError.USER_MOD_FAIL,"修改失败");
        }
    }
}
